/**
 @Name：大学生计算机课程平时成绩管理系统 学生管理
 @Author：dingwen
 */

layui.define(['table', 'form'], function (exports) {
    let $ = layui.$
        , table = layui.table
        , setter = layui.setter
        , form = layui.form;

    let total = 0;

    $(function () {
        /*获取总记录条数*/
        $.ajax({
            url: layui.setter.baseUrl + '/student/total',
            type: 'get',
            dateType: "json",
            contentType: 'application/json',
            success: function (res) {
                if (res.code === 200) {
                    total = res.data;
                    // 表格数据渲染
                    table.render({
                        elem: '#LAY-student-manage'
                        , page: true
                        , url: layui.setter.baseUrl + '/student/info/page'
                        , toolbar: '#toolbarDemo'
                        , title: '学生信息表'
                        , parseData: function (res) {
                            return {
                                "code": 0,
                                "msg": res.message,
                                "count": total,
                                "data": res.data
                            };
                        }
                        , request: {
                            pageName: 'pageNum'
                            , limitName: 'pageSize'
                        }
                        , cols:
                            [[
                                {type: 'checkbox', fixed: 'left'}
                                , {field: 'studentId', width: 200, title: '学号', sort: true}
                                , {field: 'studentName', width: 100, title: '姓名',}
                                , {field: 'studentGender', width: 100, title: '性别', templet: '#student-gender'}
                                , {field: 'studentImg', width: 200, title: '头像', templet: '#student-img'}
                                , {field: 'studentQq', title: 'QQ', minWidth: 200}
                                , {field: 'studentWechat', title: '微信', minWidth: 200}
                                , {field: 'clazzId', width: 200, title: '班级编号', sort: true}
                                , {field: 'clazzName', title: '名称', minWidth: 200}
                                , {field: 'clazzYear', title: '年级', minWidth: 100}
                                , {field: 'teacherId', title: '班主任教工号', minWidth: 200}
                                , {
                                    field: 'teacherName',
                                    title: '班主任',
                                    width: 100,
                                    style: 'background-color: #009688; color: #fff;'
                                }
                                , {field: 'teacherWechat', title: '班主任电话', minWidth: 200}
                                , {field: 'studentPassword', title: '登录密码', templet: '#student-pass', width: 100}
                                , {
                                    title: '操作',
                                    width: 200,
                                    align: 'center',
                                    fixed: 'right',
                                    toolbar: '#layuiadmin-app-student-bar'
                                }
                            ]]
                    });
                } else {
                    layer.msg("失败");
                }
            }
        });
    });

    //监听
    table.on('tool(LAY-student-manage)', function (obj) {
        let data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('确定删除 ' + data.clazzYear + "级" + data.clazzName + data.studentName + '？', function (index) {
                $.ajax({
                    url: setter.baseUrl + `/student/remove/${data.studentId}`,
                    dataType: "json",
                    type: "delete",
                    contentType: 'application/json',
                    xhrFields: {
                        withCredentials: true
                    },
                    success: function (res) {
                        if (res.code === 200) {
                            layer.msg("成功");
                        } else {
                            layer.msg("失败");
                        }
                    },
                    error: function (e) {
                        layer.msg("失败");
                    }
                });
                obj.del();
                layer.close(index);
            });
        } else if (obj.event === 'reset') {
            layer.confirm('确定重置 ' + data.clazzYear + "级" + data.clazzName + data.studentName + '的密码吗？', function (index) {
                let student = {
                    studentId: data.studentId,
                    studentPassword: 123456
                };
                $.ajax({
                    url: setter.baseUrl + '/student/modify',
                    data: JSON.stringify(student),
                    dataType: "json",
                    type: "put",
                    contentType: 'application/json',
                    xhrFields: {
                        withCredentials: true
                    },
                    success: function (res) {
                        if (res.code === 200) {
                            layer.msg("重置成功，密码为 123456");
                        } else {
                            layer.msg("失败");
                        }
                    },
                    error: function (e) {
                        layer.msg("失败");
                    }
                });
                layer.close(index);
            });
        }
    });
    exports('student', {})
});
